package fm.dongman.modellib.source

import android.util.Log
import fm.dongman.animefm.contractlib.BaseDataSource
import fm.dongman.animefm.contractlib.IModel
import fm.dongman.animefm.contractlib.contract.ChatListContract
import fm.dongman.modellib.RemoteHelper
import fm.dongman.modellib.StaticArea.StaticUrl
import fm.dongman.modellib.remote.RemoteOperationHelper
/**
 * 私信列表
 * Created by KUIGE on 2017/11/10.
 */

class ChatListRepository private constructor() : ChatListContract.IChatListDataSource {
    private var mId: String? = null // 用户 id
    private var isRemote:Boolean = true
    override fun setUserId(id: String) {
        mId = id
    }

    override fun getData(callback: BaseDataSource.LoadSourceCallback<List<IModel.IMsgModel>>) {
        if (isRemote) {
            val jsonObject = RemoteHelper.instance.parsePostUrl(StaticUrl.CHART_LIST, mapOf(Pair(SESSION_KEY, StaticUrl.TOKEN_KEY)))
            if (jsonObject == null) {
                callback.onDataLoadFiled()
                return
            }
            callback.onDataLoaded(RemoteOperationHelper.getOperationHelper().downCharList(jsonObject))
        }else callback.onDataLoadFiled()
    }

    override fun saveData(dataModel: List<IModel.IMsgModel>) {}

    override fun refreshData() {}

    override fun offLine() {
        isRemote = !isRemote
    }

    companion object {
        val SESSION_KEY = "session_key" // 字段 token
        private var SINGLETON: ChatListRepository? = null

        val instance: ChatListRepository
            get() {
                if (SINGLETON == null) {
                    SINGLETON = ChatListRepository()
                }
                return SINGLETON!!
            }
    }
}
